<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/3/20
 * Time: 上午11:58
 *
 *
 * 功能：标记预售7天发货，但是过了7天还没发货的订单需要发送企业微信通知
 *
 *
 */

//已弃用

ini_set('display_errors', 1);
ini_set('memory_limit', -1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderGoods as mOrderGoods;
use Biz\model\GoodsExtendInfo as mGoodsExtendInfo;
use Biz\model\trade\OrderPackage as mOrderPackage;
use Biz\service\ShopInfo as sShopInfo;
use Doraemon\amqp\Queue as aQueue;
use Biz\cache\OrderHelper as cOrderHelper;


//获取所有未发货订单
//条件是 (A=（订单状态=未发货   支付时间到目前已经超过7天）   OR B（订单状态=部分发货   支付时间到目前已经超过7天）)
//以下条件同时要满足没有申请退款或退款中的SPU
//如果是A，则查询订单里的商品SPU，是否有预售商品且发货时间为7天内的，有，则提醒
//如果是B，则查询未发货的SPU，是否有预售商品且发货时间为7天内的，有，则提醒

$nowTime = date('Y-m-d H:i:s');
$startPayTime = '2018-06-01 00:00:00';
$stopPayTime = date('Y-m-d H:i:s', strtotime('-7 days'));

if ($nowTime > '2018-12-21 18:15:20') {
    $startPayTime = strtotime($stopPayTime) - 300;
}


$where = [
    'pay_time[<]' => $stopPayTime,
    'pay_time[>=]' => $startPayTime,
    'status' => [mOrder::STATUS_WAIT_SEND, mOrder::STATUS_SEND_PART_OUT]
];

$orderList = mOrder::select('*', $where);

if (empty($orderList)) {
    throw new \Exception('无数据');
}

foreach ($orderList as $orderInfo) {
    $whereSkuIds = [];

    if ($orderInfo['status'] == mOrder::STATUS_SEND_PART_OUT) {

        $skuIds = mOrderPackage::getAllPackageSkuIds($orderInfo['order_no']);

        if (empty($skuIds)) {
            continue;
        }

        $whereSkuIds['sku_id[!]'] = $skuIds;

    }


    $orderGoods = mOrderGoods::select('*', array_merge($whereSkuIds, [
        'order_no' => $orderInfo['order_no'],
        'refund_amount' => 0
    ]));

    if (!empty($orderGoods)) {
        $goodsIds = array_unique(array_column($orderGoods, 'goods_id'));

        checkGoodsAndSendMsg($goodsIds, $orderInfo['pay_time'], $orderInfo['order_no'], $orderInfo['is_package_delivery'], $orderInfo['shop_id']);
    }

}


function checkGoodsAndSendMsg($goodsIds, $payTime, $orderNo, $isPackageDelivery, $shopId)
{
    $goodsList = mGoodsExtendInfo::select(['goods_id', 'shipping_time'], [
        'goods_id' => $goodsIds,
        'shipping_time_type' => mGoodsExtendInfo::SHIPPING_TIME_TYPE_PRE_SALE,
        'shipping_time[>=]' => 7
    ]);

    if (empty($goodsList)) {
        return false;
    }

    sendMsg($orderNo, $isPackageDelivery, $shopId);

}

function sendMsg($orderNo, $isPackageDelivery, $shopId)
{
    $shopInfo = sShopInfo::getShopInfo($shopId, ['info']);
    $content = vsprintf('[7天预售订单发货超时提醒] %s  的订单：%s %s 超过发货时间，请跟进处理。', [
        $shopInfo['name'],
        $orderNo,
        $isPackageDelivery ? '部分商品' : '部分商品'
    ]);

    //无需重复发送
    $canSend = cOrderHelper::canSendAgainShipTimeOutToWxNotice($orderNo);

    if (!$canSend) {
        return false;
    }

    cOrderHelper::sendShipTimeOutToWxNotice($orderNo);

    aQueue::insertQueue("queue/Wechatnotice/toTag", [
        "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_SHIP_TIMEOUT,
        "content" => $content
    ], aQueue::QUEUE_BIZ);
}